﻿using DigDes.DSchool.SUPS.DataAccess.Interface;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DigDes.DSchool.SUPS.DataAccess.Database
{
    public class FileLoaderDAC
    {
        // TODO: вынести общий функционал для подобных классов доступа к БД в общий класс BaseDAC

        // Сама строка хранится в app.config (web.config) файле исполняемого файла, который дёрнет метод этой библиотеки классов.
        // В нашем случае это DigDes.DSchool.SUPS.Test.App файл App.Config.

        private static readonly string ConnectionStringKey = "SUPSDB"; 

        public static void DoSomething(MyLoader1DTO row)
        {
            var connectionString = ConfigurationManager.ConnectionStrings[ConnectionStringKey].ConnectionString;
            using (var sqlConn = new SqlConnection(connectionString))
            {
                using (var sqlCmd = sqlConn.CreateCommand())
                {
                    sqlCmd.CommandText = "insert into SomeTable (CarNumber, Weight, ArrivalStation, CargoCode, ConsigneeCode, CarParkType, DispatchStation, OperationCode, OperationDate, OperationYear,OperationTime, OperationStation, RoadOfDelivery, RoadOfReception, TrainIndex,TrainNumber) values (@vagonnum, @weight, @destStation, @goodsCode, @recieverCode, @vagonParkType, @depatureStation," +
                    "@operationCode, @operationDate, @operationYear, @operationTime, @stationCommissionOperation, @roadDelivery, @roadRecieve,"+
                    "@trainIndex, @trainNum)";

                    sqlCmd.Parameters.AddWithValue("@vagonnum", row.Vagonnum);
                    sqlCmd.Parameters.AddWithValue("@weight", row.Weight);
                    sqlCmd.Parameters.AddWithValue("@destStation", row.DestStation);
                    sqlCmd.Parameters.AddWithValue("@goodsCode", row.GoodsCode);
                    sqlCmd.Parameters.AddWithValue("@recieverCode", row.RecieverCode);
                    sqlCmd.Parameters.AddWithValue("@vagonParkType", row.VagonParkType);
                    sqlCmd.Parameters.AddWithValue("@depatureStation", row.DepatureStation);
                    sqlCmd.Parameters.AddWithValue("@operationCode", row.OperationCode);
                    sqlCmd.Parameters.AddWithValue("@operationDate", row.OperationCode);
                    sqlCmd.Parameters.AddWithValue("@operationYear", row.OperationTime);
                    sqlCmd.Parameters.AddWithValue("@operationTime", row.OperationTime);
                    sqlCmd.Parameters.AddWithValue("@stationCommissionOperation", row.StationCommissionOperation);
                    sqlCmd.Parameters.AddWithValue("@roadDelivery", row.RoadDelivery);
                    sqlCmd.Parameters.AddWithValue("@roadRecieve", row.RoadRecieve);
                    sqlCmd.Parameters.AddWithValue("@trainIndex", row.TrainIndex);
                    sqlCmd.Parameters.AddWithValue("@trainNum", row.TrainNum);

                    sqlConn.Open();
                    sqlCmd.ExecuteNonQuery();
                }
            }
        }

        public static void DoSomething2(IEnumerable<MyLoader1DTO> rows)
        {
            // Делается по аналогии с предыдущим методом
            throw new NotImplementedException();
        }
    }
}
